<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jqGrid Demo</title>

<script
	src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools-yui-compressed.js"
	type="text/javascript"></script>
<script src="http://i-math.appspot.com/static/js/jquery-1.3.2.min.js"
	type="text/javascript"></script>
<!-- and at end the jqGrid Java Script file -->
<script src="http://i-math.appspot.com/static/js/jquery.jqGrid.js"
	type="text/javascript"></script>
</head>
<body>


<link rel="stylesheet" type="text/css" media="screen"
	href="http://i-math.appspot.com/static/css/grid-themes/sand/grid.css" />



<script type="text/javascript">
// We use a document ready jquery function.
var $j = jQuery.noConflict();

var twoVarialbleStaticData=[
                            {id:'1'},
                            {id:'2'},
                            {id:'3'},
                            {id:'4'},
                            {id:'5'},
                            {id:'6'},
                            {id:'7'},
                            {id:'8'},
                            ];

var twoVarialbleStaticAddEvent=function(){
	var target=$$('#twoVarialbleStaticlist input');
	if(target!=null){
		target.addEvent('change', function(){
			twoVarialbleStaticUpdate();
		});
	}
};

var twoVarialbleStaticUpdate=function(){
	var model=new Array();
	//load
	var strBase="#twoVarialbleStaticlist ";
	for(var i=0;i<twoVarialbleStaticData.length;i++){
		var strX=strBase.concat("#",i+1,"_x");
		var strY=strBase.concat("#",i+1,"_y");
		var strN=strBase.concat("#",i+1,"_n");
		var x=$$(strX);
		var y=$$(strY);
		var n=$$(strN);
		
			var xNum=parseFloat(x.get('value'));
			var yNum=parseFloat(y.get('value'));
			var nNum=parseFloat(n.get('value'));
			var nx=xNum*nNum;
			var ny=yNum*nNum;
			var nx2=xNum*nNum*nNum;
			var ny2=yNum*nNum*nNum;
			var b1= isNaN(xNum);
			var b2= isNaN(nNum);
			var b3= isNaN(yNum);
			
		if(!b1 && !b2 && !b3){
			var temp=new Object();
			temp.x=xNum;
			temp.y=yNum;
			temp.n=nNum;
			temp.nx=nx;
			temp.ny=ny;
			temp.nx2=nx2;
			temp.ny2=ny2;
			model.push(temp);

			twoVarialbleStaticData[i].nx=nx;
			twoVarialbleStaticData[i].ny=ny;
			$j('#twoVarialbleStaticlist').setRowData(i+1,twoVarialbleStaticData[i]);
			$('')
		}
	}
	//setSUM
	var xSum = 0;
	var ySum=0;
	var nSum=0;
	var nxSum=0;
	var nySum=0;
	var nx2Sum=0;
	var ny2Sum=0;
	var xmean=0;
	var ymean=0;
	var xs=0;
	var ys=0;
	var xs2=0;
	var ys2=0;
	
	if(model.length!=0){
		mode=model[0].x;
		for(var i=0;i<model.length;i++){
			xSum+=model[i].x;
			ySum+=model[i].y;
			nSum+=model[i].n;
			nxSum+=model[i].nx;
			nySum+=model[i].ny;
			nx2Sum+=model[i].nx2;
			ny2Sum+=model[i].ny2;
			}
		xmean=nxSum/nSum;
		ymean=nySum/nSum;
		for(var i=0;i<model.length;i++){
			xs2+=model[i].n*(model[i].x-xmean)*(model[i].x-xmean);
		}
		xs2=xs2/nSum;
		for(var i=0;i<model.length;i++){
			ys2+=model[i].n*(model[i].y-ymean)*(model[i].y-ymean);
		}
		ys2=ys2/nSum;
		xs=Math.sqrt(xs2);
		ys=Math.sqrt(ys2);
		$j('#twoVarialbleStaticlist').setRowData(twoVarialbleStaticData.length+1,{x:xSum,y:ySum,n:nSum,nx:nxSum,ny:nySum});
		$('twoVarialbleStaticXMean').set('text',xmean);
		$('twoVarialbleStaticYMean').set('text',ymean);
		$('twoVarialbleStaticXS').set('text',xs);
		$('twoVarialbleStaticYS').set('text',ys);
		$('twoVarialbleStaticXS2').set('text',xs2);
		$('twoVarialbleStaticYS2').set('text',ys2);
		}
};

var getSkewness=function(model,mean){
	var up=0;
	var down=0;
	for(var i=0;i<model.length;i++){
		for(var n=0;n<model[i].n;n++){
			up+=(model[i].x-mean)*(model[i].x-mean)*(model[i].x-mean);
			down+=(model[i].x-mean)*(model[i].x-mean);
		}
	}
	var g=up/n/(Math.pow(down/n,1.5));
	return g;
};

var getkurtosis=function(model,mean){
	var up=0;
	var down=0;
	for(var i=0;i<model.length;i++){
		for(var n=0;n<model[i].n;n++){
			up+=(model[i].x-mean)*(model[i].x-mean)*(model[i].x-mean)*(model[i].x-mean);
			down+=(model[i].x-mean)*(model[i].x-mean);
		}
	}
	var g=up/n/(Math.pow(down/n,2))-3;
	return g;
};

jQuery("#twoVarialbleStaticlist").ready(function() {
	jQuery("#twoVarialbleStaticlist").jqGrid({
	datatype: "clientSide",
    colNames:['#','x', 'y', 'n','nx','ny'],
    colModel:[
        {name:'id',index:'id', width:40},
        {name:'x',index:'x',  editable:true , width:60},
        {name:'y',index:'y',  editable:true , width:60},
    	    {name:'n',index:'n', editable:true, width:60},
        {name:'nx',index:'nx',  width:60},
        {name:'ny',index:'ny',  width:60}],
    imgpath: 'http://i-math.appspot.com/static/css/grid-themes/sand/images',
    viewrecords: true,
    caption: "二维统计",
    shrinkToFit:true,
    	height:230
	}
	);

	
	for(var i=0;i<twoVarialbleStaticData.length;i++){
		$j('#twoVarialbleStaticlist').addRowData(i+1,twoVarialbleStaticData[i]);
		$j('#twoVarialbleStaticlist').editRow(i+1,true);
	}
	$j('#twoVarialbleStaticlist').addRowData(twoVarialbleStaticData.length+1,{id:'总和:'});
	twoVarialbleStaticAddEvent();
});



</script>

<table id="twoVarialbleStaticlist" class="scroll" cellpadding="0"
	cellspacing="0"></table>
<table cellpadding="0" cellspacing="0" style="width: 400px" border="0">
	<tbody style="font-size: small">
		<tr>
			<td style="width: 50%">x平均数：<span id="twoVarialbleStaticXMean"></span></td>
			<td style="width: 50%">y平均数：<span id="twoVarialbleStaticYMean"></span></td>
		</tr>
		<tr>
			<td style="width: 50%">x标准差：<span id="twoVarialbleStaticXS"></span></td>
			<td style="width: 50%">y标准差：<span id="twoVarialbleStaticYS"></span></td>
		</tr>
		<tr>
			<td style="width: 50%">x标准差平方：<span id="twoVarialbleStaticXS2"></span></td>
			<td style="width: 50%">y标准差平方：<span id="twoVarialbleStaticYS2"></span></td>
		</tr>
	</tbody>
</table>

</body>
</html>
